<?php
////////////////////////////////////////////////////////////////////////////
//                                                                        //
// NOTICE OF COPYRIGHT                                                    //
//                                                                        //
// Dean`s Office for Moodle                                               //
// Электронный деканат                                                    //
// <http://deansoffice.ru/>                                               //
//                                                                        //
// Copyright (C) 2012-2999  Kirill Krasnoschekov (Кирилл Краснощёков)     //
//                                                                        //
// This program is free software: you can redistribute it and/or modify   //
// it under the terms of the GNU General Public License as published by   //
// the Free Software Foundation, either version 3 of the Licensen.        //
//                                                                        //
// This program is distributed in the hope that it will be useful,        //
// but WITHOUT ANY WARRANTY; without even the implied warranty of         //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          //
// GNU General Public License for more details.                           //
//                                                                        //
// You should have received a copy of the GNU General Public License      //
// along with this program.  If not, see <http://www.gnu.org/licenses/>.  //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

global $DOF;
$DOF->modlib('widgets')->webform();


/**
 * Abstract form that is used in the guestbook
 *
 * @author Kirill Krasnoschekov
 */
abstract class guestbook_form extends dof_modlib_widgets_form 
{
    
    protected $dof;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        // Alias for DOF controller
        $this->dof = $this->_customdata->dof;
    }
    
    /**
     * Get rendered form 
     * 
     * @return string Rendered form
     * @author Kirill Krasnoschekov
     */
    public function to_html() 
    {
        return $this->_form->toHtml();
    }
    
    /**
     * Update submitted data of the form
     * 
     * @param array $submission Hash array of submitted form values
     * @param array $files
     * @author Kirill Krasnoschekov
     */
    public function update_submission($submission, $files) 
    {
        return $this->_form->updateSubmission($submission, $files);
    }
    
    /**
     * Reset submitted values to empty
     * 
     * @author Kirill Krasnoschekov
     */
    public function reset_submission() 
    {
        return $this->update_submission(null, null);
    }
}



/**
 * Form for messages
 * 
 * @author Kirill Krasnoschekov
 */
class guestbook_message_form extends guestbook_form 
{

    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    public function definition() 
    {
        global $DOF;
        
        $mform =& $this->_form;
        
        // Elements
        $mform->addElement('text', 'name', $DOF->get_string('name', 'gb'));
        $mform->addElement('textarea', 'text', $DOF->get_string('text', 'gb'),
                array('cols' => 40, 'rows' => 10));

        // Rules
        $mform->addRule('name', $DOF->get_string('field_required', 'gb'), 'required');
        $mform->addRule('text', $DOF->get_string('field_required', 'gb'), 'required');

        // Filters
        $mform->applyFilter('name', 'trim');
        $mform->applyFilter('text', 'trim');
                
        // Buttons
        $mform->addElement('reset', 'reset', $DOF->get_string('reset', 'gb'));
        $mform->addElement('submit', 'submit', $DOF->get_string('submit', 'gb'));
    }
    
    /**
     * Process gorm data
     */
    public function process($addvars)
    {
        if ( $this->is_submitted() AND $data = $this->get_data() )
        {
            // Check data
            if ( isset($data->name)  )
            
            // Insert record for the new message
            
            // Clear the form
            $this->reset_submission();
            return true; 
        }
        
        return false;
    }
}



/**
 * Form for message updating
 */
class guestbook_message_update_form extends guestbook_message_form 
{
    
    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    public function definition() 
    {
        global $DOF;
        
        $mform =& $this->_form;
        
        // Header
        $mform->addElement('header','message_update_form', 
                $DOF->get_string('message_update_form', 'gb'));

        // Hidden: mode and message id
        $mform->addElement('hidden', 'mode', 'update');
        $mform->addElement('hidden', 'msgid');
        
        parent::definition();
    }
}



/**
 * Form for adding message
 * 
 * @author Kirill Krasnoschekov
 */
class guestbook_message_add_form extends guestbook_message_form 
{
    
    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    public function definition() 
    {
        global $DOF;
        
        $mform =& $this->_form;
        
        // Header
        $mform->addElement('header','message_add_form', 
                $DOF->get_string('message_add_form', 'gb'));
                
        // Message id
        $mform->addElement('hidden', 'mode', 'add');

        parent::definition();
    }
    
}



/**
 * Form for comments
 * 
 * @author Kirill Krasnoschekov
 */
class guestbook_comment_form extends guestbook_form 
{

    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    public function definition() 
    {
        global $DOF;
        
        $mform =& $this->_form;
        
        // Header
        $mform->addElement('header','comment_form', 
                $DOF->get_string('comment_add_form', 'gb'));

        // Mode and message id 
        $mform->addElement('hidden', 'mode', 'add');
        $mform->addElement('hidden', 'msgid');
        
        // Fields
        $mform->addElement('text', 'name', $DOF->get_string('name', 'gb'));
        $mform->addElement('textarea', 'text', $DOF->get_string('text', 'gb'),
                array('cols' => 40, 'rows' => 10));
                
        // Buttons
        $mform->addElement('reset', 'reset', $DOF->get_string('reset', 'gb'));
        $mform->addElement('submit', 'submit', $DOF->get_string('submit', 'gb'));
        

        // Rules
        $mform->addRule('name', $DOF->get_string('field_required', 'gb'), 'required');
        $mform->addRule('text', $DOF->get_string('field_required', 'gb'), 'required');

        // Filters
        $mform->applyFilter('name', 'trim');
        $mform->applyFilter('text', 'trim');        
    }
}